package com.bianmaba.controllers.datacollection;

import com.bianmaba.services.datacollection.quartiz.QuartzTaskManager;
import org.quartz.JobDataMap;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "datacollection/schedule")
public class ScheduleController {
    @Autowired
    private QuartzTaskManager quartzTaskManager;

    @RequestMapping("schedule")
    public Trigger schedule(String name, String group, String cronExpression) throws ClassNotFoundException {
        Class clazz = Class.forName("com.bianmaba.jobs.DefaultJob");
        JobDataMap data = new JobDataMap();
        data.put("description", "scheduleJob is running...");

        return quartzTaskManager.scheduleJob(clazz, name, group, cronExpression, data);
    }


    @RequestMapping("unschedule")
    public void unscheduleJob(String name, String group) throws ClassNotFoundException, SchedulerException {
        quartzTaskManager.unscheduleJob(name, group);
    }

    @RequestMapping("reschedule")
    public Trigger reschedule(String name, String group, String cronExpression) throws ClassNotFoundException {
        return quartzTaskManager.rescheduleJob(name, group, "0/5 * * * * ?");
    }
}
